package org.tooyoung.fourone.live.adapters;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;
import org.tooyoung.fourone.R;
import org.tooyoung.fourone.live.activitys.PlayActivity;
import org.tooyoung.fourone.live.models.Room;

import java.util.List;

public class MyRoomListAdapter extends RecyclerView.Adapter<MyRoomListAdapter.MyViewHolder> {


    private List<Room> rooms;
    private Context context;

    public MyRoomListAdapter(List<Room> rooms, Context context) {
        this.rooms = rooms;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.liveitem, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.live_item_iv.setScaleType(ImageView.ScaleType.CENTER);
       final MyViewHolder myholder=holder;
        Picasso.with(holder.live_item_iv.getContext())
                .load(rooms.get(position).getHeadimg())
                .config(Bitmap.Config.RGB_565)
                .placeholder(R.mipmap.empty_work_lighn)
                .error(R.mipmap.empty_work_lighn)
                .into(holder.live_item_iv, new Callback() {
                    @Override
                    public void onSuccess() {
                     myholder.live_item_iv.setScaleType(ImageView.ScaleType.FIT_XY);
                    }

                    @Override
                    public void onError() {
                        myholder.live_item_iv.setScaleType(ImageView.ScaleType.CENTER);
                    }
                });
        holder.live_item_iv.setTag(rooms.get(position));
        holder.live_nickname.setText(rooms.get(position).getNickname());
    }

    @Override
    public int getItemCount() {
        int ret =0;
        if (rooms != null) {
            ret =rooms.size();
        }
        return ret;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView live_item_iv;
        private TextView live_nickname;
        public MyViewHolder(View itemView) {
            super(itemView);
            live_item_iv= (ImageView) itemView.findViewById(R.id.live_item_iv);
            live_item_iv.setOnClickListener(this);
            live_nickname= (TextView) itemView.findViewById(R.id.live_nickname);
        }

        @Override
        public void onClick(View view) {
            Object tag = view.getTag();
            if (tag != null) {
                if (tag instanceof Room)
                {
                    Room room = (Room) tag;
//                    Log.d("wwwwwwwww", "onClick: "+room.getHeadimg());
//                    EventBus.getDefault().post(room);
                    Intent intent = new Intent(context, PlayActivity.class);
                    intent.putExtra("data",room);
                    context.startActivity(intent);
                }
            }
        }
    }
}
